package com.cn.delan.jdbc;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Hashtable;

import org.json.simple.JSONObject;

import com.cn.delan.core.util.ClassUtil;
import com.cn.delan.core.util.StringUtil;
/**
 * 记录集对象
 * @author BingGo
 * @version 1.0
 */
public class RowList extends ArrayList<Row>{

	private static final long serialVersionUID = -4598855073851813874L;
	/**
	 * 页长度
	 */
	private int pageCount;
	/**
	 * 页码
	 */
	private int pageNo;
	/**
	 * 总记录数
	 */
	private int total;
	/**
	 * 总页数
	 */
	private int pageTotal;
	/**
	 * 记录集头部信息
	 */
	private Header header;
	
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public int getPageTotal() {
		return pageTotal;
	}
	public void setPageTotal(int pageTotal) {
		this.pageTotal = pageTotal;
	}
	public Header getHeader() {
		return header;
	}
	public void setHeader(Header header) {
		this.header = header;
	}
	/**
	 * 获取某条记录,标号从0开始
	 * @param index 标号
	 * @return 指定的记录
	 */
	public Row getRow(int index){
		return this.get(index);
	}
	/**
	 * 在结果集中加入一条记录
	 * @param row 记录信息
	 * @return 添加成功返回true,否则返回false
	 */
	public boolean addRow(Row row){
		return this.add(row);
	}
	/**
	 * 将此对象转换成为指定类的list集合，类中对应元素必须全为String
	 * @param clazz 指定类
	 * @return 封装后的集合
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 */
	public <T> ArrayList<T> toClassList(Class<T> clazz) throws InstantiationException, IllegalAccessException, SecurityException, IllegalArgumentException, NoSuchMethodException, InvocationTargetException{
		ArrayList<T> list = new ArrayList<T>();
		for(Row<String,Object> row:this){
			T t = ClassUtil.create(clazz);
			for(String key:row.keySet()){
				ClassUtil.set(t, key.toLowerCase(), row.getCell(key));
			}
			list.add(t);
		}
		return list;
	}
	/**
	 * 将此对象转换成为指定类，类中对应元素必须全为String
	 * @param clazz 指定类
	 * @return 返回封装后的类
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 */
	public <T> T toClass(Class<T> clazz) throws InstantiationException, IllegalAccessException, SecurityException, IllegalArgumentException, NoSuchMethodException, InvocationTargetException{
		T t = ClassUtil.create(clazz);
		if(this.size()>=1){
			Row<String,Object> row = this.getRow(0);
			for(String key:row.keySet()){
				ClassUtil.set(t, key.toLowerCase(), row.getCell(key));
			}
		}
		return t;
	}
	/**
	 * 将list转换为json格式
	 * @return
	 */
	public JSONObject toJson(){
		JSONObject obj = new JSONObject();
		obj.put("total", this.getTotal());
		ArrayList<Hashtable<String,String>> rows = new ArrayList<Hashtable<String,String>>();
		for(Row<String,Object> row:this){
			Hashtable<String,String> item = new Hashtable<String,String>();
			//默认来自同表
			boolean inSameTable = true;
			String tmpTableId = null;
			Header header = this.getHeader();
			for(String key:row.keySet()){
				item.put(key.toLowerCase(), row.getCellValue(key));
				//获取并判断是否来源同一表
				if(!inSameTable){
					//得出来自非同表判断结果,无需再判断
					continue;
				}
				String tableId = header.get(key).getTableId();
				if(StringUtil.isEmpty(tmpTableId)){
					tmpTableId = tableId;
				}else{
					if(!tmpTableId.equals(tableId)){
						inSameTable = false;
					}
				}
			}

			//当所有字段来源于同表时才能进行编辑和删除
			if(inSameTable){
				ColumnDetail detail = this.getHeader().get("id");
				String controlType = "";
				controlType += "<a class='editA' href=\"javascript:void(0);\" onclick=\"edit('"+detail.getTableId()+"','"+row.getCellValue(detail.getColId())+"');\"><img style='border:0px;margin:0px 6px;' src='core/themes/icons/pencil.png'></a>";
				controlType += "<a class='delA' href=\"javascript:void(0);\" onclick=\"del('"+detail.getTableId()+"','"+row.getCellValue(detail.getColId())+"');\"><img style='border:0px;margin:0px 6px;' src='core/themes/icons/cancel.png'></a>";
				item.put("controlType", controlType);
			}
			rows.add(item);
		}
		obj.put("rows", rows);
		return obj;
	}

}
